Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
#### The Common Dialog Control Before continuing with the project, we need to take a look at the Common Dialog control, one of the custom controls provided with Visual Basic. You added a Common Dialog control to the Baby Editor form earlier. What exactly is this control? It’s very cool and will save you scads of work. The Common Dialog control provides your Visual Basic applications with several of the frequently needed Windows dialog boxes: Open, Save As, Font, Color, Help, and Printer. You specify which dialog box you want by the method used to display it. If a particular dialog box offers options—a Color choice in the Font dialog box, for example—you can control their display by setting certain control properties before displaying the dialog box. Similarly, selections the user makes in the dialog box are returned to the calling program in properties. The Common Dialog control is a terrific timesaver. While you could certainly construct these dialog boxes yourself using Visual Basic controls, why bother? Remember our most basic rule: Don’t reinvent the wheel. As you’ll see, using the Common Dialog control is quite easy. It also gives you the advantage of having your program’s dialog boxes look identical to those used by many other Windows programs that also use the Common Dialogs (which are actually a part of Windows, not Visual Basic). The first task we will use the Common Dialog box for is letting the user select a file to open. #### Opening A File Opening a file means reading the data from a text file on disk and displaying it in the program’s Text Box. This process consists of two main parts: • Using the Common Dialog control to let the user select the file • Using Visual Basic’s file access commands to open, read, and close the file We’ll deal with these issues in turn. First, which files should we make available to the user? Rather than simply listing all of the files in the selected folder, the better course is to restrict the file listing to those files the user is likely to be interested in—in other words, text files. We already took care of this task in the Form_Load event procedure (Listing 11.2) when we initialized the Common Dialog control’s Filter property as follows:
    Filter = “Text files (*.txt) | *.txt”
    Filter = Filter & “|Batch files (*.bat) | *.bat”
    Filter = Filter & “|INI files (*.ini) | *.ini”
    Filter = Filter & “|All files (*.*) | *.*”
    CommonDialog1.Filter = Filter
With this code, we have actually loaded four different filters into the dialog box: one that will display only text files (*.TXT) , one for batch files (*.BAT) , and so on. When the dialog box is displayed, the user will be able to select the desired filter in the List Files of Type list. Note that one of the filters will list all files—a good idea, because we cannot be sure which extensions will be associated with text files on the user’s system. Once the filter has been specified, displaying the Open dialog box requires only two lines of code:
    CommonDialog1.FilterIndex = 1
    CommonDialog1.ShowOpen
The first line specifies the filter that will be in effect initially. The second line displays the Common Dialog with the ShowOpen method, specifying that the control is to act as an Open dialog box. Execution pauses until the user closes the dialog box, at which time the selected file name can be retrieved from the Common Dialog’s FileName property. If the user canceled the dialog box without selecting a file, this property will contain a blank string. Once we have the name of the file to open, we must open it, read the data, and close the file. To open a file, you must supply the file name and a number that will be associated with the file. Because a particular number can be associated with only one open file at one time, and because you have no way of knowing which numbers are in use (Windows can have multiple processes going on simultaneously, so processes other than your program can have numbers associated with open files), use the FreeFile function to be sure you select an unused number for your file. The two lines of code required to open a file (whose name is stored in the variable FileName ) are as follows:
    FileNum = FreeFile
    Open FileName For Input As FileNum
Note the syntax of the Open statement. It specifies the file name that is being opened for input (because we are just reading the file, not writing anything to it) and the number of the file. Once the file is open, use this number to refer to it in subsequent program statements. Once the file is open, we will read the text one line at a time. The Basic statement designed for this task is Line Input, which reads a single line of text from the file. A “line” is defined as a sequence of characters that ends with either a carriage return (CR) character by itself or combined with a line feed (LF) character. If we repeatedly execute Line Input until we reach the end of the file, we will read all lines of text in the file. Each line is placed in a buffer, which is simply a string variable used for temporary storage of data. This buffer is then added to the end of another buffer, using the concatenation operator &. This second buffer then accumulates the entire contents of the file. Finally, the second buffer is copied to the Text Box’s Text property. There’s one minor complication. Windows text files are stored with a single CR character at the end of each line. While the CR character is sufficient for the Line Input statement to detect the end of the line, the Text Box requires the CRLF combination to break a line. If we simply copied the text file’s contents directly to the Text Box, we would end up with one very long line of text. To break the text into separate lines, we must add the CRLF combination to the end of each line before putting it in the buffer. We obtain these characters with Visual Basic’s built-in Chr$ function, which returns the character corresponding to a specific numerical ASCII code. The ASCII codes for CR and LF are 13 and 10, respectively, so we can create a string variable holding the CRLF combination as follows:
    CRLF = Chr$(13) + Chr$(10)
Now we can just concatenate this variable onto the end of the buffer after each line is read from the file. An even easier method is to use the built-in constant vbCRLF to obtain a CRLF combination. We also need some way of telling when we have reached the end of the file. The EOF function serves this purpose. If you pass a file number as its argument, this function returns True if the end of the file has been reached, False otherwise. We can set up a loop as follows (expressed in pseudocode):
    Do While Not end of file
        Get next line
        Add to buffer
    Loop
The final steps include using the Close statement to close the file, copying the text to the Text Box, and changing the form’s caption to display the name of the file—and we’re finished. The code for the File|Open command’s Click event procedure is shown in Listing 11.9. Note that this procedure first calls the SaveChanges function, which ensures that no editing changes are lost. We’ll look at its code later. For now, it’s enough to know that the function returns True if there are no unsaved changes. If unsaved changes exist, the user is given the option either to save or discard them. SaveChanges returns False if the user decides to cancel, in which case execution exits the mnuFileOpen_Click procedure, leaving the original contents of the editor unchanged.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.